<template>
	<view class="content">
		<view v-if="isLogin">
			<image class="headicon" @click="changeHeadIcon" :src="'http://localhost:8090/foodapp/'+userInfo.icon"></image>
			<view class="text-area" v-if="isLogin">
				<text class="title">{{userInfo.nickname}}</text>
			</view>
		</view>
		<view v-if="!isLogin">
			<image class="headicon" :src="userInfo.icon"></image>
			<view class="text-area">
				<button type="primary" size="mini" @click="toLogin">点击登录</button>
			</view>
		</view>
		<view class="entry-wrapper">
			<view class="item">
				<text>个人信息</text><image src="../../static/more.png" mode=""></image>
			</view>
			<view class="item">
				<text>订单列表</text><image src="../../static/more.png" mode=""></image>
			</view>
			<view class="item">
				<text>个人信息</text><image src="../../static/more.png" mode=""></image>
			</view>
			<view class="item">
				<text>个人信息</text><image src="../../static/more.png" mode=""></image>
			</view>
		</view>
		<button type="primary" size="default" style="width: 100%;" :disabled="!isLogin" @click="logout">退出</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					userid: null,
					icon: '',
					nickname: '',
					username: null
				},
				isLogin: false
			}
		},
		onLoad() {
	
		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			changeHeadIcon() {
							let userInfo = uni.getStorageSync('userInfo')
							uni.chooseImage({
								count: 1,
								success: (choosRes) => {
									let filePath = choosRes.tempFilePaths[0]
									console.log(choosRes)
									uni.uploadFile({
										url: this.$baseUrl+'user/upload_headpic',
										filePath: filePath,
										name: 'file',
										formData: {
											id: this.userInfo.id
										},
										success: (uploadRes) => {
										    // 更新本地存储，更新页面
											console.log(uploadRes)
											let resData = JSON.parse(uploadRes.data)
											console.log(resData)
											// uni.setStorageSync('userInfo', result.data)
											this.userInfo.icon = resData.data.icon
										}
									})
								}
							})
						},
			logout(){
				uni.setStorageSync('userInfo',null)
				this.getUserInfo()
				
			},
			toLogin() {
				uni.navigateTo({
					url: './login'
				})
			},
			getUserInfo() {
				let userInfo = uni.getStorageSync('userInfo')
				if (userInfo) {
					this.userInfo = userInfo
					this.isLogin = true
					console.log('用户已登录，读取成功');
				} else {
					this.userInfo.username = null
					this.userInfo.nickname = null
					this.userInfo.icon = '../../static/my.png'
					this.isLogin = false
				}
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 20rpx;
	}
	
	.headicon {
		height: 200rpx;
		width: 200rpx;
		margin-top: 80rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		border-radius: 50%;
	}
	
	.text-area {
		display: flex;
		justify-content: center;
	}
	
	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.entry-wrapper{
		width: 90%;
		/* background-color: #888; */
		margin-top:50rpx;
	}
	.entry-wrapper .item{
		/*水平线性布局*/
		display: flex;
		flex-direction: row;
		/*子元素两端对齐*/
		justify-content: space-between;
		/*高度80rpx*/
		height: 80rpx;
		/*子元素在交叉轴上（y）居中对齐*/
		align-items: center;
		/*添加上下边框线*/
		border-bottom: #333 solid 1rpx;
	}
	.entry-wrapper .item:first-child{
		/*给第一个item加上边框线*/
		border-top: #333 solid 1rpx;
	}
	.entry-wrapper .item image{
		width: 48rpx;
		height: 48rpx;
	}
	button{
		width: 95%;
		margin-top: 50rpx;
	}
</style>